package com.tabjin.rfidsocket.authority.shiro.controller;

import com.tabjin.rfidsocket.authority.shiro.model.vo.UserVO;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author tabjin
 * create at 2020/12/17 23:58
 * @program rfid_socket
 * @description
 */
@RestController
@RequestMapping("/users")
public class ShiroController {
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult login() {
        // TODO 跳转至登录页面
        return TabJsonResult.ok();
    }

    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult index() {
        // TODO 跳转至index页面
        return TabJsonResult.ok();
    }

    /**
     * 登出
     *
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult logout() {
        Subject subject = SecurityUtils.getSubject();// 取出验证主体
        if (subject != null) {// 登出
            subject.logout();
        }
        // TODO 跳转至login页面
        return TabJsonResult.ok("退出登录成功");
    }

    /**
     * 未认证
     *
     * @return {TabJsonResult} 标准JSON
     */
    @RequestMapping(value = "/unauthorized", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult unauthorized() {
        Subject subject = SecurityUtils.getSubject();
        UserVO userVO = (UserVO) subject.getPrincipal();
        // TODO 跳转未认证界面
        return TabJsonResult.ok("未认证，跳转至未授权页面");
    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public TabJsonResult edit() {
        // TODO 跳转编辑界面
        return TabJsonResult.ok("有权限edit");
    }

    /**
     * admin
     *
     * @return
     */
    @RequestMapping(value = "/admin", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult admin() {
        return TabJsonResult.ok("admin");
    }

    /**
     * 登录处理
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/loginUser", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult loginUser(@RequestParam("username") String username,
                                   @RequestParam("password") String password,
                                   HttpSession session) {
        if (StringUtils.isBlank(username)) {
            return TabJsonResult.errorMessage("请输入用户名");
        }

        if (StringUtils.isBlank(password)) {
            return TabJsonResult.errorMessage("请输入密码");
        }

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // Shiro 认证逻辑
        Subject subject = SecurityUtils.getSubject();
        UserVO userVO = new UserVO();
        try {
            subject.login(token);// 调用AuthRealm的相关进行认证
            userVO = (UserVO) subject.getPrincipal();// 主体，即登录用户
            session.setAttribute("user", userVO);// 这和AuthRealm中是一一对应的，也就是授权的时候在session中取得值也是User对象
            // TODO 返回index页面
        } catch (Exception e) {
            e.printStackTrace();
            // TODO 返回登录页面
        }
        return TabJsonResult.ok(userVO);
    }
}
